import { createSlice } from "@reduxjs/toolkit";
import todolist from "../../utile/todo";
import {  init } from "./todoSlice";


let slice=createSlice({
  name:'addtodo',
  initialState:'',
  reducers:{
    title:(state,action)=>{
      return state=action.payload
    }
  }
})

let reducer=slice.reducer

export let {title}=slice.actions

export default reducer

export let asyncAddTodo=(titles)=>{
  return async dispatch=>{
    let {data}=await todolist({
      method:'post',
      url:'/todos',
      data:{
        title:titles
      }
    })
    // console.log(data);
    let aaa=[]
    aaa.push(data)
    dispatch(title(''))
    dispatch(init(aaa))
  }
}